package com.dtu.snapdroid;

import java.util.ArrayList;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;

import com.dtu.ImagesExtras.ImageAndText;
import com.dtu.ImagesExtras.ImageGridAdapter;
import com.dtu.XMLExtras.ReadXMLImages;

public class GridAlbum extends Activity { //extends ListActivity {
	/** Called when the activity is first created. */
	
	public static String tagPickerResult;
	private ReadXMLImages mImageDownloadHandler;
	private GridView g;
	private Button filterbutton;
	private ArrayList<ImageAndText> imageList;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.gridalbum);
	    
	    mImageDownloadHandler = new ReadXMLImages();
	    mImageDownloadHandler.setPath("http://loftur.com/Pics/results.xml"); 
	    g = (GridView) findViewById(R.id.gridview);
	    
	    update();
	    g.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				new PictureInflater(GridAlbum.this, imageList.get(position).getImageUrl(), 2, imageList.get(position).getText()).show()	;
			}
		});
	    
		filterbutton = ( Button ) findViewById( R.id.filterbutton);
		filterbutton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				FilterPicker fp = new FilterPicker(GridAlbum.this);
				fp.setOnDismissListener(new DialogInterface.OnDismissListener(){
					public void onDismiss(DialogInterface dialog) {
						mImageDownloadHandler.setPath("http://loftur.com/taggedpics.xml");
						update();
					}
				});
				fp.show();
			}			
		});	    
	}
	
	private void update(){
		imageList = mImageDownloadHandler.getImages();
		g.setAdapter(new ImageGridAdapter(getApplicationContext(),GridAlbum.this,imageList, g));
	}
	


}